<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------


if (!function_exists('fail')) {
    function fail($errorMessage = null) {
        $data = [
            "errno"=> "100001",
            "statusCode"=> 200,
            "errmsg"=> "系统错误，请稍后重试。"
        ];

        if (!empty($errorMessage)) {
            $message = require dirname(__FILE__).'/locale/zh-cn.php';
            if (isset($message[$errorMessage])) {
                $error = $message[$errorMessage];
                $data = [
                    "errno"=> (string)$error[0],
                    "statusCode"=> 200,
                    "errmsg"=> $error[1]
                ];
            }
        }

        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }
}

//api请求成功返回
if (!function_exists('success')) {
    function success($data)
    {
        return json_encode([
            "errno" => 0,
            "errmsg" => "",
            "statusCode"=> 200,
            "data" => $data
        ], JSON_UNESCAPED_UNICODE);
    }
}

// 应用公共文件.

function curl($url = '', $post = [], $headers = [], $ssl = [])
{

    $method = 'GET';
    if($post)
    {
        $method = 'POST';
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    if (1 == strpos("$".$url, "https://"))
    {
        if($ssl){

            curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,true);
            curl_setopt($curl, CURLOPT_SSLCERT, $ssl['cert']);
            curl_setopt($curl, CURLOPT_SSLKEY, $ssl['key']);

        }else{
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
    }

    if($method)
    {
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    }

    return curl_exec($curl);
}

// 密码加密
function sign($pass){
    return md5($pass."haj22!*%");
}

// 签名
function auth($param = []){

    ksort($param);

    $str = '';
    foreach ($param as $k=> $v){
        $v = urldecode($v);
        $str .= $k.$v;
    }

    $str .= "jako000";

    return md5(rawurlencode($str));


}

// 汉字转数字
function str2num($str){

    preg_match("(千|万)", $str, $match);

    $unit = $match[0];

    $num = str_replace($unit, '', $str);

    $b = 1;
    if($unit == "千"){
        $b = 1000;
    }elseif ($unit == "万"){
        $b = 10000;
    }

    return $num * $b;

}

function save_img($img, $path=""){

    $path = "static/uploads/".$path.'/'.date('Ymd');

    if(!is_dir($path)){
        mkdir($path, 0777, true);
    }

    $file = $path.'/'.uniqid('a').".png";

    file_put_contents($file, $img);

    return "/$file";
}

// 获取ip

function getIp(){

    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
        $ip = getenv("REMOTE_ADDR");
    else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
        $ip = $_SERVER['REMOTE_ADDR'];
    else
        $ip = "unknown";

    return $ip;
}

// 数组转xml
function array2xml($data, $root = true)
{
    $str="";
    if($root)$str .= "<xml>";
    foreach($data as $key => $val){
        if(is_array($val)){
            $child = array2xml($val, false);
            $str .= "<$key>$child</$key>";
        }else{
            if (is_numeric($val)){
                $str.= "<$key>$val</$key>";
            }else{
                $str.= "<$key><![CDATA[$val]]></$key>";
            }

        }
    }
    if($root)$str .= "</xml>";
    return $str;
}

// 计算距离
function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){

    $EARTH_RADIUS = 6370.996; // 地球半径系数
    $PI = 3.1415926;

    $radLat1 = $latitude1 * $PI / 180.0;
    $radLat2 = $latitude2 * $PI / 180.0;

    $radLng1 = $longitude1 * $PI / 180.0;
    $radLng2 = $longitude2 * $PI /180.0;

    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;

    $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
    $distance = $distance * $EARTH_RADIUS * 1000;

    if($unit==2){
        $distance = $distance / 1000;
    }

    return round($distance, $decimal);

}